package com.tz.crm.controller.contract;

import com.tz.crm.entity.ContractTemplate;
import com.tz.crm.entity.User;
import com.tz.crm.entity.dto.Contract;
import com.tz.crm.entity.dto.ContractFrom;
import com.tz.crm.entity.dto.Payment;
import com.tz.crm.service.ContractService;
import com.tz.crm.service.ContractTemplateService;
import com.tz.crm.service.PaymentService;
import com.tz.crm.service.UserService;
import com.tz.crm.utils.DateUtil;
import com.tz.crm.utils.MsgErrorUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by sztzedu on 17-3-14.
 */
@Controller
@RequestMapping(value = "/contract")
public class InitContractController {

    @Resource
    private ContractTemplateService contractTemplateService;

    @Resource
    private UserService userService;

    @Resource
    private ContractService contractService;

    @Resource
    private PaymentService paymentService;

    @RequestMapping(value = "/selectTem")
    public String ListContractTemplate(HttpServletRequest request,String contractId){
        List<ContractTemplate> contractTemplates = contractTemplateService.getUsingTemplates();
        request.getSession().setAttribute("contractTemplates",contractTemplates);
        request.getSession().setAttribute("contractId",contractId);
        User user = userService.getByLogin("admin","admin");
        request.getSession().setAttribute("user",user);
       /* Contract contract = new Contract();
        request.getSession().setAttribute("contract",contract);*/
        return "createContract";
    }

    @RequestMapping(value = "/save")
    public String saveContract(HttpServletRequest request,ContractFrom contractFrom, BindingResult bindingResult2){

        if(bindingResult2.hasErrors()){
            Map<String,String> erros = MsgErrorUtil.getErrorMap(bindingResult2);
            /*model.addAttribute("erros",erros);*/
            request.getSession().setAttribute("erros",erros);
            //model.addAttribute("contract",contract);
            return "createContract";
        } else{

            String[] strs=request.getParameter("selected").split(":");
            String[] inputs = request.getParameter("amount").split(":");
            System.out.println("amounts===="+inputs);
            Set<com.tz.crm.entity.Payment> paymentList = new HashSet<>(strs.length);
            com.tz.crm.entity.Payment p;
            for (int i=0;i<paymentList.size();i++) {
                p=new com.tz.crm.entity.Payment();
                p.setPayName(com.tz.crm.entity.Payment.PayName.valueOf(strs[i]));
                p.setAmount(Double.parseDouble(inputs[3*i]));
               p.setDueDate(DateUtil.parse(inputs[3*i+1],"yyyy-MM-dd"));
               p.setRemark(inputs[3*i+2]);
                paymentList.add(p);
            }

            System.out.println("dyhjd");
                com.tz.crm.entity.Contract contract1 = new com.tz.crm.entity.Contract();
                contract1.setInNum(request.getParameter("contractDto.inNum"));
                contract1.setExNum(request.getParameter("contractDto.exNum"));
                contract1.setAmount(Double.valueOf(request.getParameter("contractDto.amount")));
                contract1.setCustomerName(request.getParameter("customers"));
                contract1.setEndDate(DateUtil.parse(request.getParameter("contractDto.endDate"),"yyyy-MM-dd"));
                contract1.setStartDate(DateUtil.parse(request.getParameter("contractDto.startDate"),"yyyy-MM-dd"));
                contract1.setSignDate(DateUtil.parse(request.getParameter("contract.signDate"),"yyyy-MM-dd"));
                contract1.setRemark(request.getParameter("contractDto.remark"));
                ContractTemplate contractTemplate = contractTemplateService.get(request.getParameter("contractDto.templateId"));
                contract1.setContractTemplate(contractTemplate);
                User user = userService.get("username",request.getParameter("contract.customerName"));
                contract1.setSignUser(user);
//                contractService.save(contract1);
                System.out.println(contract1);
           /* System.out.println(request.getParameter("Payment"));*/

           /* System.out.println("strs = ====" + strs);*/

           /* com.tz.crm.entity.Payment p=new com.tz.crm.entity.Payment();*/
           /* p.setAmount(Double.valueOf(request.getParameter("amount")));
            p.setRemark(request.getParameter("remark"));
            p.setDueDate(DateUtil.parseDate(request.getParameter("dueDate"),"yyyy-MM-dd"));*/
                    /*com.tz.crm.entity.Payment payment1 = paymentService.get("payName",request.getParameter("PayName"));*/
                    /*p.setPayName(payment1.getPayName());*/
                    contract1.setPayPlan(paymentList);
                contractService.save(contract1);
                return "success";
            }
        }
    }



